/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.util.parser;

import java.util.ArrayList;
import java.util.List;

import org.vnetcon.blobdb.database.SQLException;
import org.vnetcon.blobdb.database.util.ColumnInfo;

public class UpdateTableParser {

	private List<String> elements = null;

	private String table = null;
	private ColumnInfo colInfo = null;
	private String rowid = null;
	private ArrayList<String> updateColumns = new ArrayList<String>();

	public UpdateTableParser(List<String> elements) throws SQLException{
		this.elements = elements;
	}

	public String getRowId(){
		return this.rowid;
	}

	public String getTableName(){
		return this.table;
	}

	public void setColumnInfo(ColumnInfo ci){
		this.colInfo = ci;
	}

	public ArrayList<String> getUpdateColumns(){
		return this.updateColumns;
	}
	
	public ColumnInfo getColumnInfo(){
		return this.colInfo;
	}

	public void initTable() throws SQLException {
		int i = 0;
		if(elements.size() < 3){
			throw new SQLException("Sql statement is too short!");
		}
		table = elements.get(1);
	}
	
	public void init() throws SQLException {
		int i = 0;
		if(elements.size() < 3){
			throw new SQLException("Sql statement is too short!");
		}
		String buf = null;
		table = elements.get(1);
		buf = elements.get(2);
		if(!buf.toUpperCase().equals("SET")){
			throw new SQLException("Expecting 'SET' after 'update tablename'. Found: " + buf);
		}
		try{
			i = initColumns();
		}catch(Exception ex){
			throw new SQLException("Failed to parse update statements columns: " + ex.getMessage());
		}
		i++;
		if(elements.size() <= i){
			throw new SQLException("Sql statement is too short!");
		}
		buf = elements.get(i);
		if(!buf.toUpperCase().equals("ROWID")){
			throw new SQLException("Expected 'ROWID'. Found: " + buf);
		}
		i++;
		if(elements.size() <= i){
			throw new SQLException("Sql statement is too short!");
		}
		buf = elements.get(i);
		if(!buf.toUpperCase().equals("=")){
			throw new SQLException("Expected '=' after rowid. Found: " + buf);
		}
		i++;
		if(elements.size() <= i){
			throw new SQLException("Sql statement is too short!");
		}
		this.rowid = elements.get(i);

		if(rowid.startsWith("'") && rowid.endsWith("'")){
			rowid = rowid.substring(1, rowid.length() - 1);
		}
		
	}
	
	private int initColumns() throws SQLException {
		int i = 3;
		String buf = elements.get(i);
		if(buf.toUpperCase().equals("WHERE")){
			throw new SQLException("No columns to update.");
		}
		
		while(!buf.toUpperCase().equals("WHERE")){
			buf = elements.get(i);
			String colName = buf;
			i++;
			String equal = elements.get(i);
			String value = null;
			if(!equal.equals("=")){
				throw new SQLException("Expected '='. Found: " + equal);
			}
			i++;
			value = elements.get(i);
			if(value.startsWith("'") && value.endsWith("'")){
				value = value.substring(1, value.length() - 1);
			}
			colInfo.setColValue(colName, value);
			updateColumns.add(colName);
			i++;
			buf = elements.get(i);
			if(!buf.equals(",") && !buf.toUpperCase().equals("WHERE")){
				throw new SQLException("Expected ',' or 'WHERE'. Found: " + equal);
			}
			
			if(buf.equals(",")){
				i++;
			}
			
		}
		return i;
	}
	
}
